#ifndef DIALOGUE_H
#define DIALOGUE_H

#include "text.h"
#include "dialogue_box.h"

#define CHAR_WIDTH 8
#define CHAR_HEIGHT 8

u8 determineCharIndex(char c);

class dialogue
{
public:
	u16 _cursorPosition;
	u8 _x;
	u8 _y;
	u8 _width;
	u8 _height;
	dialogue();
	void RenderDBox(u16 *fb);
	void RenderText(u16 *fb);
	void RenderTile(u16 *fb, u8 x, u8 y, u8 frame);
};

dialogue::dialogue()
{
	_x = 8;
	_y = 88;
	_width = 30;
	_height = 12;
	_cursorPosition = 0;
}

void dialogue::RenderDBox(u16 *fb)
{
	// Starting point in buffer
	
	u8 xoff = 0;
	u8 yoff = 0;
	
	u8 piece = 0;
	u8 poff = 0;
	
	u8 frame = 0;
	
	for(uint16 n = 0; n < _width*_height; n++)
	{
		if (piece == 0) frame = 0 + poff;
		if (piece > 0 && piece < _width) frame = 1 + poff;
		if (piece == _width-1) frame = 2 + poff;
		RenderTile(fb, _x+xoff, _y+yoff, frame);
		
		xoff = xoff+CHAR_WIDTH;
		if(xoff > (_width-1)*CHAR_WIDTH) 
		{
			xoff = 0;
			yoff = yoff+CHAR_HEIGHT;
		}
		piece++;
		if(piece == _width) piece = 0;
		if(n == _width-1 || n == _width*_height - _width-1) poff += 3;
	}
}

void dialogue::RenderTile(u16 *fb, u8 x, u8 y, u8 frame)
{
	unsigned short yy;
	unsigned short xx;
	u8 t = 0;
	
	for(yy = y; yy < y+CHAR_HEIGHT; yy++)
	{
		for(xx = x; xx < x+CHAR_WIDTH; xx++)
		{
			if(dialogue_box_sprites[frame][t] > 0) // Not transparent 
			{
				fb[yy*SCREEN_WIDTH+xx] = dialogue_box_palette[dialogue_box_sprites[frame][t]];
			}
			t++;
		}
	}
}

void RenderText(u16 *fb)
{
	
}

u8 determineCharIndex(char c)
{
	switch (c)
	{
		case 'a':
			return 0;
		case 'b':
			return 1;
	}	
	
	return 0;
}
#endif //DIALOGUE_H